package com.andaily.web.controller.mobile.account;

import com.andaily.domain.dto.account.AccountSettingDTO;
import com.andaily.infrastructure.support.LogHelper;
import com.andaily.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Account setting form controller
 *
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/account/setting")
public class AccountSettingController {

    private static LogHelper log = LogHelper.create(AccountSettingController.class);

    @Autowired
    private AccountService accountService;

    /**
     * Initialize form
     *
     * @param model ModelMap
     * @return View path
     */
    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(ModelMap model) {
        AccountSettingDTO accountSettingDTO = accountService.loadAccountSettingDTO();
        log.debug("Load account setting: " + accountSettingDTO);

        model.addAttribute("accountSetting", accountSettingDTO);
        return "account/setting/account_setting";
    }

    /**
     * Submit form
     *
     * @param accountSetting AccountSettingDTO
     * @param result         BindingResult
     * @param model          ModelMap
     * @return View path
     */
    @RequestMapping(method = RequestMethod.POST)
    public String submit(@ModelAttribute("accountSetting") AccountSettingDTO accountSetting, BindingResult result, ModelMap model) {
        boolean updateResult = accountService.updateAccountSetting(accountSetting);
        log.debug("Update account setting result: " + updateResult);
        return "redirect:/m/account/setting?result=" + updateResult;
    }

    /**
     * Restore deleted account or not action
     *
     * @param guid  Account guid
     * @param model ModelMap
     * @return View path
     */
    @RequestMapping("restore/{guid}")
    public String showRestore(@PathVariable() String guid, ModelMap model) {
        log.debug("Show restore account. guid = " + guid);
        model.addAttribute("accountGuid", guid);
        return "account/setting/account_restore_confirm";
    }

    /**
     * Clean up the account data action (delete it from DB)
     *
     * @param guid  Account guid
     * @param model ModelMap
     * @return View path
     */
    @RequestMapping("cleanup/{guid}")
    public String cleanUp(@PathVariable() String guid, ModelMap model) {
        log.debug("Clean up account. guid = " + guid);
        accountService.cleanUpAccount(guid);
        return "redirect:/m/account/setting?cleanResult=true";
    }

    /**
     * Revert account data action
     *
     * @param guid  Account guid
     * @param model ModelMap
     * @return View path
     */
    @RequestMapping("revert/{guid}")
    public String revert(@PathVariable() String guid, ModelMap model) {
        log.debug("Revert account. guid = " + guid);
        boolean result = accountService.revertAccount(guid);
        return "redirect:/m/account/setting?revertResult=" + result;
    }

}
